package com.sh.sales.happysales.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sh.polymedicine.happysale.net.entity.Region;
import com.sh.sales.happysales.R;
import com.sh.sales.happysales.ui.activity.RegionActivity;

import java.util.ArrayList;

/**
 * Created by zhouyucheng on 2017/9/18.
 */

public class RegionAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Region> regions;
    private int position;

    public RegionAdapter(RegionActivity regionActivity, ArrayList<Region> mRegions) {
        this.mContext = regionActivity;
        this.regions = mRegions;
    }




    @Override
    public int getCount() {
        return regions.size();
    }

    @Override
    public Object getItem(int i) {
        return regions.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LocationHolder mLocationHolder = null;
        if (view == null) {
            mLocationHolder = new LocationHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_location, null);
            mLocationHolder.locationName = (TextView) view.findViewById(R.id.location_name);
            view.setTag(mLocationHolder);
        } else {
            mLocationHolder = (LocationHolder) view.getTag();
        }
        Region add = regions.get(i);
        mLocationHolder.locationName.setText(add.getName());
        if (position == i) {
            mLocationHolder.locationName.setBackground(mContext.getResources().getDrawable(R.drawable.shape_locationsel));
            mLocationHolder.locationName.setTextColor(Color.WHITE);
        } else {
            mLocationHolder.locationName.setBackground(mContext.getResources().getDrawable(R.drawable.shape_location));
            mLocationHolder.locationName.setTextColor(Color.BLACK);
        }
        return view;
    }

    public void setPosition(int position) {
        this.position = position;
        notifyDataSetChanged();
    }

    class LocationHolder {
        TextView locationName;
    }
}
